package com.jarchie.androiddev.ui.adapters

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.jarchie.androiddev.R
import com.jarchie.androiddev.databinding.LayoutFeedTypeBannerBinding
import com.jarchie.androiddev.databinding.LayoutFeedTypeImageBinding
import com.jarchie.androiddev.databinding.LayoutFeedTypeVideoBinding
import com.jarchie.androiddev.model.HomeItem
import com.youth.banner.indicator.CircleIndicator

class HomeAdapter(val mList: List<HomeItem>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    companion object {
        private const val TYPE_BANNER = 0
        private const val TYPE_IMAGE = 1
        private const val TYPE_VIDEO = 2
    }

    override fun getItemViewType(position: Int): Int {
        return when (mList[position]) {
            is HomeItem.BannerItem -> TYPE_BANNER
            is HomeItem.ImageItem -> TYPE_IMAGE
            is HomeItem.VideoItem -> TYPE_VIDEO
        }
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): RecyclerView.ViewHolder {
        val bannerBinding =
            LayoutFeedTypeBannerBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        val imageBinding =
            LayoutFeedTypeImageBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        val videoBinding =
            LayoutFeedTypeVideoBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return when (viewType) {
            TYPE_BANNER -> BannerHolder(bannerBinding)
            TYPE_IMAGE -> ImageHolder(imageBinding)
            else -> VideoHolder(videoBinding)
        }
    }

    override fun onBindViewHolder(
        holder: RecyclerView.ViewHolder,
        position: Int
    ) {
        when (val item = mList[position]) {
            is HomeItem.BannerItem -> (holder as BannerHolder).bind(item)
            is HomeItem.ImageItem -> (holder as ImageHolder).bind(item)
            is HomeItem.VideoItem -> (holder as VideoHolder).bind(item)
        }
    }

    override fun getItemCount(): Int = mList.size


    @Suppress("UNCHECKED_CAST")
    inner class BannerHolder(val binding: LayoutFeedTypeBannerBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(item: HomeItem.BannerItem) {
            binding.banner.setAdapter(ImageAdapter(item.bannerList))
                .indicator = CircleIndicator(binding.root.context)
        }
    }

    inner class ImageHolder(val binding: LayoutFeedTypeImageBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(item: HomeItem.ImageItem) {
            binding.author1.avatar.setImageResource(R.drawable.icon_avatar)
            binding.author1.name.text = item.images[0].nickname
            Glide.with(binding.root.context).load(item.images[0].image).into(binding.beauty1)
            binding.label1.btn.text = item.images[0].desc
            binding.author2.avatar.setImageResource(R.drawable.icon_avatar)
            binding.author2.name.text = item.images[1].nickname
            Glide.with(binding.root.context).load(item.images[1].image).into(binding.beauty2)
            binding.label2.btn.text = item.images[1].desc
        }
    }

    inner class VideoHolder(val binding: LayoutFeedTypeVideoBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(item: HomeItem.VideoItem) {

        }
    }

}